const devicesId = "859459085" 
const api_key = "QB6OAO=1m10T7Ay90MuBVzlxsx4=" 
var lighting=0
var x=0
var limit=350
Page({
    data: {
      rule: 'up',
      lighting:120,
      items: [
        { name: 'up', value: '高于门限报警', checked: 'ture' },
        { name: 'down', value: '低于门限报警' },
      ]
    },
    start(){
        if(x==0){
        console.log(`your deviceId: ${devicesId}, apiKey: ${api_key}`)
        const timer = setInterval(() => {
        this.getDatapoints().then(()=>{this.warning()})
        }, 500)
        x=timer
        wx.showLoading({
          title: '加载中'
        })
        this.getDatapoints().then(() => {
          wx.hideLoading()
        }).catch((err) => {
          wx.hideLoading()
          console.error(err)
          clearInterval(timer) 
        })}
      },
      stop(){
        clearInterval(x) 
        wx.hideToast()
        x=0
      },
     warning(){
        console.log(lighting)
        if(this.data.rule == "up"){
            if(lighting<=limit){
            wx.showToast({
                title: '光强过高，请及时减小光强',
                icon: 'none',
                duration: 1000,
              })
        }}
        if(this.data.rule == "down"){
            if(lighting>=limit){
            wx.showToast({
                title: '光强过高，请及时减小光强',
                icon: 'none',
                duration: 1000,
              })
        }}
    },
    radioChange: function (e) {
      //保存报警规则到当前页面的数据
      if (e.detail.value != "") {
        this.setData({
          rule: e.detail.value
        })
      }
      console.log(this.data.rule)
    },

    getDatapoints: function () {
        return new Promise((resolve, reject) => {
          wx.request({
            url: `https://api.heclouds.com/devices/${devicesId}/datapoints?datastream_id=Lighting&
     limit=20`,
            header: {
              'content-type': 'application/json',
              'api-key': api_key
            },
            success: (res) => {
              const status = res.statusCode
              const response = res.data
              if (status !== 200) { 
                reject(res.data)
                return ;
              }
              if (response.errno !== 0) {
                reject(response.error)
                return ;
              }
              if (response.data.datastreams.length === 0) {
                reject("当前设备无数据, 请先运行硬件实验")
              }
              resolve({
                lighting:response.data.datastreams[0].datapoints.reverse()[0]
              })
                lighting= response.data.datastreams[0].datapoints.reverse()[0].value
                this.setData({
                    lighting:response.data.datastreams[0].datapoints.reverse()[0].value
                })
            },
            fail: (err) => {
              reject(err)
            }
          })
        })
      },
    change: function (e) {
      //当有输入时激活发送按钮，无输入则禁用按钮
      if (e.detail.value != "") {
          limit= e.detail.value
      }
    }
  })